﻿using System;

namespace Gqq.CSharpindepth2.DesignPatterns.Observer
{
   public class Cat:ISubject
    {
       public event ActionHandler AniAction;

       /// <summary>
       /// 通知的方法，就是调用注册的事件
       /// </summary>
       public void Notify()
       {
           if (AniAction != null)
               AniAction();
       }

       /// <summary>
       /// 注册的方法，Cat的所有Observer都可以通过该方法注册到Cat上
       /// 等待Cat 通知他们。
       /// </summary>
       /// <param name="a"></param>
       public void Register(ActionHandler a)
       {
           AniAction += a;
       }

       public void Cry()
       {
           Console.WriteLine("cat is crying...");
           Notify();
       }

       public static void DoTest()
       {
           var cat = new Cat();
           var master = new Master(cat);
           var mouse = new Mouse(cat);
           var master2 = new Master(cat);
           cat.Cry();
       }
    }
}
